package com.puman.hello;


import com.puman.hello.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
public class HelloController {

    private final Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private DiscoveryClient client;
    @RequestMapping("/hello")
    public String index(){
        ServiceInstance instance =client.getLocalServiceInstance();
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId());
        return "hello world";
    }
    @RequestMapping("/hello1")
    public String hello1(@RequestParam String name){
        ServiceInstance instance =client.getLocalServiceInstance();
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId());
        return "hello "+name;
    }
    @RequestMapping("/hello2")
    public User hello2(@RequestHeader String name, @RequestHeader long id){
        ServiceInstance instance =client.getLocalServiceInstance();
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId());
        return new User(name,id);
    }
    @RequestMapping("/hello3")
    public String hello3(@RequestBody User user){
        ServiceInstance instance =client.getLocalServiceInstance();
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId());
        return "hello "+user.getName()+","+user.getId();
    }
    @RequestMapping("/users")
    public User users(Long id){
        ServiceInstance instance =client.getLocalServiceInstance();
        System.out.println(System.currentTimeMillis());
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId());
        if (id==1){
            User user = new User();
            user.setId(id);
            user.setName("张三");
            return user;
        }else{
            User user = new User();
            user.setId(id);
            user.setName("李四");
            return user;
        }

    }

    @RequestMapping("/helloParam")
    public String helloParam(String name){
        ServiceInstance instance =client.getLocalServiceInstance();
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId()+"params"+name);
        return "hello "+name;
    }
    @RequestMapping("/helloParams")
    public String helloParams(String name,int age){
        ServiceInstance instance =client.getLocalServiceInstance();
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId()+"params"+name);
        return "hello "+name+" age:"+age;
    }
    @RequestMapping("/postParam")
    public String postParam(@RequestBody Map<String,Object> reqMap){
        ServiceInstance instance =client.getLocalServiceInstance();
        StringBuffer params = new StringBuffer();
        for (Map.Entry<String,Object> entry:reqMap.entrySet()){
            params.append(entry.getKey()+":"+entry.getValue());
        }
        logger.info("hello,host:"+instance.getHost()+" ,"+"service_id:"+instance.getServiceId()+"params"+params);
        return "hello "+params;
    }
}
